package mall.kotlin.com.myapplication.android5_0.notification;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PixelFormat;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.graphics.Palette;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RemoteViews;

import mall.kotlin.com.myapplication.MainActivity;
import mall.kotlin.com.myapplication.R;

public class NotificationActivity extends AppCompatActivity {

    private NotificationManager mNotificationManager;
    private NotificationCompat.Builder mBuilder;

    private View view;
    private WindowManager wm;
    private boolean showWm = true;//默认是应该显示悬浮通知栏
    private WindowManager.LayoutParams params;
    private static final int PUSH_NOTIFICATION_ID = (0x001);
    private static final String PUSH_CHANNEL_ID = "PUSH_NOTIFY_ID";
    private static final String PUSH_CHANNEL_NAME = "PUSH_NOTIFY_NAME";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notification);
        initWindowManager();
        Button button1 = findViewById(R.id.bt1);
        Button button2 = findViewById(R.id.bt2);
        Button button3 = findViewById(R.id.bt3);
        Button button4 = findViewById(R.id.bt4);
        Button button5 = findViewById(R.id.bt5);
        CheckBox box1 = findViewById(R.id.cb1);
        CheckBox box2 = findViewById(R.id.cb2);
        CheckBox box3 = findViewById(R.id.cb3);
        init();
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //普通通知
                Intent intent = new Intent(NotificationActivity.this, MainActivity.class);
                PendingIntent pendingIntent = PendingIntent.getActivity(NotificationActivity.this, 0, intent, 0);
                mBuilder.setContentIntent(pendingIntent);
                mBuilder.setSmallIcon(R.mipmap.ic_launcher);
                mBuilder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.map));
                mBuilder.setAutoCancel(true); //自动取消
                mBuilder.setContentTitle("普通通知");
                mBuilder.setContentText("我是内容");
                mBuilder.setOnlyAlertOnce(true);//只提醒一次
                mBuilder.setDefaults(NotificationCompat.DEFAULT_VIBRATE); //默认的震动
                mNotificationManager.notify(1, mBuilder.build());
            }
        });
        button2.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
            @Override
            public void onClick(View v) {
                //折叠式通知
                Intent intent = new Intent(NotificationActivity.this, MainActivity.class);
                PendingIntent pendingIntent = PendingIntent.getActivity(NotificationActivity.this, 0, intent, 0);
                mBuilder.setContentIntent(pendingIntent);
                mBuilder.setSmallIcon(R.mipmap.ic_launcher);
                mBuilder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.map));
                mBuilder.setAutoCancel(true); //自动取消
                mBuilder.setContentTitle("折叠式通知");
                mBuilder.setContentText("我是折叠式内容");
                RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.remot);
                Notification build = mBuilder.build();
                build.bigContentView = remoteViews;
                mNotificationManager.notify(1, build);
            }
        });
        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //悬挂式通知 android 5.0新增的方式(该方法并好试)
                /*Notification.Builder builder3 = new Notification.Builder(NotificationActivity.this);
                Intent intent3 = new Intent(NotificationActivity.this, CardViewActivity.class);
                PendingIntent pendingIntent3 = PendingIntent.getActivity(NotificationActivity.this, 0, intent3, 0);
                builder3.setContentIntent(pendingIntent3);
                builder3.setSmallIcon(R.mipmap.ic_launcher);
                builder3.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
                builder3.setAutoCancel(true);
                builder3.setContentTitle("悬挂通知");
                builder3.setTicker("悬浮通知222");
                builder3.setDefaults(~0);
                builder3.setPriority(Notification.PRIORITY_HIGH);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    builder3.setVisibility(Notification.VISIBILITY_PUBLIC);
                }
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//SDK版本大于等于21才有悬挂式通知栏

                    Intent XuanIntent = new Intent(NotificationActivity.this, MainActivity.class);
                    XuanIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                    PendingIntent xuanpengdIntent = PendingIntent.getActivity(NotificationActivity.this, 0, XuanIntent, PendingIntent.FLAG_CANCEL_CURRENT);
                    builder3.setFullScreenIntent(xuanpengdIntent, true);
                }


                mNotificationManager.notify(2, builder3.build());*/
                view = LayoutInflater.from(NotificationActivity.this).inflate(R.layout.remot, null);
                //在这里你可以解析你的自定义的布局成一个View
                if (showWm) {
                    wm.addView(view, params);
                    showWm = false;
                } else {
                    wm.updateViewLayout(view, params);
                }


                view.setOnTouchListener(new View.OnTouchListener() {
                    public boolean onTouch(View v, MotionEvent event) {
                        switch (event.getAction()) {
                            case MotionEvent.ACTION_DOWN:
                                wm.removeViewImmediate(view);
                                view = null;
                                break;
                            case MotionEvent.ACTION_MOVE:

                                break;
                        }
                        return true;
                    }
                });


            }
        });

        button4.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.O)
            @Override
            public void onClick(View v) {
                //8.0android系统下
                //首先创建渠道  ChannelId 渠道Id "1"  Channelname 渠道名称 "Channel1"
                /*NotificationChannel channel = new NotificationChannel("1", "channel1", NotificationManager.IMPORTANCE_DEFAULT);
                channel.enableLights(true);//是否在桌面icon右上角显示小红点
                channel.setLightColor(Color.RED);
                channel.setShowBadge(true);
                mNotificationManager.createNotificationChannel(channel);

                //发送通知
                Notification.Builder builder = new Notification.Builder(NotificationActivity.this, "1");//channelId
                builder.setSmallIcon(android.R.drawable.stat_notify_chat)
                        .setContentTitle("xxx")
                        .setContentText("xxx")
                        .setNumber(3); //久按桌面图标时允许的此条通知的数量
                mNotificationManager.notify(0x1234, builder.build());

                //删除NotificationChannel
                NotificationChannel notificationChannel = mNotificationManager.getNotificationChannel("1");
                mNotificationManager.deleteNotificationChannel("1");*/


            }
        });
        box1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                /*//创建通知渠道
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    NotificationChannel channel = new NotificationChannel(PUSH_CHANNEL_ID, PUSH_CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
                    if (mNotificationManager != null) {
                        mNotificationManager.createNotificationChannel(channel);
                    }
                }
                //创建通知并显示
                NotificationCompat.Builder builder = new NotificationCompat.Builder(NotificationActivity.this,PUSH_CHANNEL_ID);
                Intent notificationIntent = new Intent(NotificationActivity.this, MainActivity.class);
                notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                PendingIntent pendingIntent = PendingIntent.getActivity(NotificationActivity.this, 0, notificationIntent, 0);
                builder.setContentTitle("通知标题")//设置通知栏标题
                        .setContentIntent(pendingIntent) //设置通知栏点击意图
                        .setContentText("通知内容")
                        .setNumber(2)
                        .setTicker("通知内容") //通知首次出现在通知栏，带上升动画效果的
                        .setWhen(System.currentTimeMillis())//通知产生的时间，会在通知信息里显示，一般是系统获取到的时间
                        .setSmallIcon(R.mipmap.ic_launcher)//设置通知小ICON
                        .setChannelId(PUSH_CHANNEL_ID)
                        .setDefaults(Notification.DEFAULT_ALL);

                Notification notification = builder.build();
                notification.flags |= Notification.FLAG_AUTO_CANCEL;
                if (mNotificationManager != null) {
                    mNotificationManager.notify(PUSH_NOTIFICATION_ID, notification);
                }*/

                String id = "my_channel_01";
                String name = "我是渠道名字";
                mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                Notification notification = null;
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    NotificationChannel mChannel = new NotificationChannel(id, name, NotificationManager.IMPORTANCE_LOW);
                    mNotificationManager.createNotificationChannel(mChannel);
                    notification = new Notification.Builder(NotificationActivity.this)
                            .setChannelId(id)
                            .setContentTitle("5 new messages")
                            .setContentText("hahaha")
                            .setSmallIcon(R.mipmap.ic_launcher).build();
                } else {
                    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(NotificationActivity.this)
                            .setContentTitle("5 new messages")
                            .setContentText("hahaha")
                            .setSmallIcon(R.mipmap.ic_launcher)
                            .setOngoing(true);
                    notification = notificationBuilder.build();
                }
            }
        });
        button5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //用palette提取颜色
                Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
                Palette.from(bitmap).generate(new Palette.PaletteAsyncListener() {
                    @Override
                    public void onGenerated(@Nullable Palette palette) {
                        Palette.Swatch vibrantSwatch = palette.getVibrantSwatch();
                        int rgb = vibrantSwatch.getRgb();
                    }
                });
            }
        });
    }

    private void init() {
        mNotificationManager = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE);
        mBuilder = new NotificationCompat.Builder(this);


    }

    private void initWindowManager() {
        wm = (WindowManager) getApplicationContext().getSystemService(
                Context.WINDOW_SERVICE);
        params = new WindowManager.LayoutParams();
        //注意是TYPE_SYSTEM_ERROR而不是TYPE_SYSTEM_ALERT
        //前面有SYSTEM才可以遮挡状态栏，不然的话只能在状态栏下显示通知栏
        params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
        params.format = PixelFormat.TRANSPARENT;
        //设置必须触摸通知栏才可以关掉
        params.flags = WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
                | WindowManager.LayoutParams.FLAG_FULLSCREEN
                | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;

        // 设置通知栏的长和宽
        params.width = wm.getDefaultDisplay().getWidth();
        params.height = 200;
        params.gravity = Gravity.TOP;
    }
}
